{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datasets import load_dataset\n",
    "import os\n",
    "import json\n",
    "\n",
    "test_dataset = load_dataset(\"json\",\n",
    "                            data_files=\"https://huggingface.co/datasets/newfacade/LeetCodeDataset/resolve/main/LeetCodeDataset-v2-test-problems.jsonl\")[\"train\"]\n",
    "print(\"Test set:\", test_dataset)\n",
    "\n",
    "train_dataset = load_dataset(\"json\",\n",
    "                            data_files=\"https://huggingface.co/datasets/newfacade/LeetCodeDataset/resolve/main/LeetCodeDataset-v2-train-problems.jsonl\")[\"train\"].filter(\n",
    "    lambda example: example[\"meta\"][\"question_id\"] not in set([d[\"question_id\"] for d in test_dataset[\"meta\"]]))\n",
    "print(\"Training set:\", train_dataset)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Only one bad data point from our tests!\n",
    "bad_data = set(['word-ladder-ii'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset = []\n",
    "for entry in train_dataset:\n",
    "    tests = {\"functional\": f\"{entry['test']}\\n\\ncheck({entry['entry_point'].strip()})\"}\n",
    "    problem =  f\"Please solve the programming task below using a self-contained code snippet in a markdown code block.\\n\\n{entry['meta']['query'].strip()}\"\n",
    "    if len(tests) == 0:\n",
    "        continue \n",
    "    new_entry = {\n",
    "        \"problem\": problem,\n",
    "        \"solutions\": entry[\"completion\"],\n",
    "        \"tests\": tests,\n",
    "    }\n",
    "    if entry['task_id'] not in bad_data:\n",
    "        dataset.append(new_entry)\n",
    "\n",
    "print(f'Dataset size: {len(dataset)}')\n",
    "\n",
    "output_dir = os.path.abspath(\"../../train/code\")\n",
    "output_file = os.path.join(output_dir, \"leetcode.json\")\n",
    "\n",
    "with open(output_file, \"w\") as f:\n",
    "    json.dump(dataset, f, indent=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset = []\n",
    "for entry in test_dataset:\n",
    "    tests = {\"functional\": f\"{entry['test']}\\n\\ncheck({entry['entry_point'].strip()})\"}\n",
    "    problem =  f\"Please solve the programming task below using a self-contained code snippet in a markdown code block.\\n\\n{entry['meta']['query'].strip()}\"\n",
    "    if len(tests) == 0:\n",
    "        continue \n",
    "    new_entry = {\n",
    "        \"problem\": problem,\n",
    "        \"solutions\": entry[\"completion\"],\n",
    "        \"tests\": tests,\n",
    "    }\n",
    "    dataset.append(new_entry)\n",
    "\n",
    "print(f'Dataset size: {len(dataset)}')\n",
    "\n",
    "output_dir = os.path.abspath(\"../../test/code\")\n",
    "output_file = os.path.join(output_dir, \"leetcode.json\")\n",
    "\n",
    "with open(output_file, \"w\") as f:\n",
    "    json.dump(dataset, f, indent=4)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "rllm",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
